home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing.plaf.basic;
-
- import com.sun.java.swing.AbstractAction;
- import com.sun.java.swing.BorderFactory;
- import com.sun.java.swing.BoxLayout;
- import com.sun.java.swing.JComboBox;
- import com.sun.java.swing.JComponent;
- import com.sun.java.swing.JDialog;
- import com.sun.java.swing.JList;
- import com.sun.java.swing.JPopupMenu;
- import com.sun.java.swing.JRootPane;
- import com.sun.java.swing.JScrollPane;
- import com.sun.java.swing.SwingUtilities;
- import com.sun.java.swing.Timer;
- import com.sun.java.swing.UIManager;
- import com.sun.java.swing.border.Border;
- import com.sun.java.swing.event.ListSelectionListener;
- import java.awt.AWTEvent;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Container;
- import java.awt.Dialog;
- import java.awt.Dimension;
- import java.awt.Point;
- import java.awt.Rectangle;
- import java.awt.Toolkit;
- import java.awt.Window;
- import java.awt.event.InputEvent;
- import java.awt.event.ItemListener;
- import java.awt.event.KeyListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.awt.event.MouseMotionListener;
- import java.beans.PropertyChangeListener;
- import java.util.EventObject;
-
- public class BasicComboPopup extends JPopupMenu implements ComboPopup {
- protected JComboBox comboBox;
- protected JList list;
- protected JScrollPane scroller;
- protected boolean valueIsAdjusting = false;
- protected MouseMotionListener mouseMotionListener;
- protected MouseListener mouseListener;
- protected KeyListener keyListener;
- protected ListSelectionListener listSelectionListener;
- protected MouseListener listMouseListener;
- protected MouseMotionListener listMouseMotionListener;
- protected PropertyChangeListener propertyChangeListener;
- protected ItemListener itemListener;
- protected Timer autoscrollTimer;
- protected boolean hasEntered = false;
- protected boolean isAutoScrolling = false;
- protected int scrollDirection = 0;
- protected static final int SCROLL_UP = 0;
- protected static final int SCROLL_DOWN = 1;
-
- public BasicComboPopup(JComboBox combo) {
- this.comboBox = combo;
- this.mouseListener = this.createMouseListener();
- this.mouseMotionListener = this.createMouseMotionListener();
- this.keyListener = this.createKeyListener();
- this.listSelectionListener = this.createListSelectionListener();
- this.listMouseListener = this.createListMouseListener();
- this.listMouseMotionListener = this.createListMouseMotionListener();
- this.propertyChangeListener = this.createPropertyChangeListener();
- this.itemListener = this.createItemListener();
- this.list = this.createList();
- this.configureList();
- this.scroller = this.createScroller();
- this.configureScroller();
- this.configurePopup();
- this.addComboBoxListeners();
- }
-
- protected void addComboBoxListeners() {
- this.comboBox.addPropertyChangeListener(this.propertyChangeListener);
- this.comboBox.addItemListener(this.itemListener);
- }
-
- protected void addListListeners() {
- this.list.addListSelectionListener(this.listSelectionListener);
- this.list.addMouseMotionListener(this.listMouseMotionListener);
- this.list.addMouseListener(this.listMouseListener);
- }
-
- protected void autoScrollDown() {
- int index = this.list.getSelectedIndex();
- int lastItem = this.list.getModel().getSize() - 1;
- if (index < lastItem) {
- this.valueIsAdjusting = true;
- this.list.setSelectedIndex(index + 1);
- this.valueIsAdjusting = false;
- this.list.ensureIndexIsVisible(index + 1);
- }
-
- }
-
- protected void autoScrollUp() {
- int index = this.list.getSelectedIndex();
- if (index > 0) {
- this.valueIsAdjusting = true;
- this.list.setSelectedIndex(index - 1);
- this.valueIsAdjusting = false;
- this.list.ensureIndexIsVisible(index - 1);
- }
-
- }
-
- protected Rectangle computePopupBounds(int px, int py, int pw, int ph) {
- Rectangle r = new Rectangle(px, py, pw, ph);
- boolean inModalDialog = this.inModalDialog();
- Rectangle absBounds;
- if (inModalDialog) {
- Dialog dlg = this.getDialog();
- if (dlg instanceof JDialog) {
- JRootPane rp = ((JDialog)dlg).getRootPane();
- Point p = ((Component)rp).getLocationOnScreen();
- absBounds = ((Component)rp).getBounds();
- absBounds.x = p.x;
- absBounds.y = p.y;
- } else {
- absBounds = ((Component)dlg).getBounds();
- }
-
- Point scrSize = new Point(absBounds.x, absBounds.y);
- SwingUtilities.convertPointFromScreen(scrSize, this.comboBox);
- absBounds.x = scrSize.x;
- absBounds.y = scrSize.y;
- } else {
- Dimension scrSize = Toolkit.getDefaultToolkit().getScreenSize();
- absBounds = new Rectangle();
- Point p = new Point(0, 0);
- SwingUtilities.convertPointFromScreen(p, this.comboBox);
- absBounds.x = p.x;
- absBounds.y = p.y;
- absBounds.width = scrSize.width;
- absBounds.height = scrSize.height;
- }
-
- if (SwingUtilities.isRectangleContainingRectangle(absBounds, r)) {
- return r;
- } else {
- Rectangle r2 = new Rectangle(0, -r.height, r.width, r.height);
- if (SwingUtilities.isRectangleContainingRectangle(absBounds, r2)) {
- return r2;
- } else if (inModalDialog) {
- SwingUtilities.computeIntersection(absBounds.x, absBounds.y, absBounds.width, absBounds.height, r);
- SwingUtilities.computeIntersection(absBounds.x, absBounds.y, absBounds.width, absBounds.height, r2);
- return r.height > r2.height ? r : r2;
- } else {
- return r2;
- }
- }
- }
-
- protected void configureList() {
- this.list.setFont(this.comboBox.getFont());
- this.list.setForeground(this.comboBox.getForeground());
- this.list.setBackground(this.comboBox.getBackground());
- this.list.setSelectionForeground(UIManager.getColor("ComboBox.selectedForeground"));
- this.list.setSelectionBackground(UIManager.getColor("ComboBox.selectedBackground"));
- this.list.setBorder((Border)null);
- this.list.setCellRenderer(this.comboBox.getRenderer());
- this.list.setRequestFocusEnabled(false);
- this.addListListeners();
- }
-
- protected void configurePopup() {
- ((Container)this).setLayout(new BoxLayout(this, 1));
- ((JPopupMenu)this).setBorderPainted(true);
- ((JComponent)this).setBorder(BorderFactory.createLineBorder(Color.black));
- ((JComponent)this).setOpaque(false);
- ((JPopupMenu)this).add(this.scroller);
- ((JComponent)this).setDoubleBuffered(true);
- ((JComponent)this).setRequestFocusEnabled(false);
- }
-
- protected void configureScroller() {
- this.scroller.setRequestFocusEnabled(false);
- this.scroller.getVerticalScrollBar().setRequestFocusEnabled(false);
- this.scroller.setBorder((Border)null);
- }
-
- protected MouseEvent convertMouseEvent(MouseEvent e) {
- Point convertedPoint = SwingUtilities.convertPoint((Component)((EventObject)e).getSource(), e.getPoint(), this.list);
- MouseEvent newEvent = new MouseEvent((Component)((EventObject)e).getSource(), ((AWTEvent)e).getID(), ((InputEvent)e).getWhen(), ((InputEvent)e).getModifiers(), convertedPoint.x, convertedPoint.y, ((InputEvent)e).getModifiers(), e.isPopupTrigger());
- return newEvent;
- }
-
- protected ItemListener createItemListener() {
- return new ComboItemListener(this);
- }
-
- protected KeyListener createKeyListener() {
- return new InvocationKeyListener(this);
- }
-
- protected JList createList() {
- return new JList(this.comboBox.getModel());
- }
-
- protected MouseListener createListMouseListener() {
- return new ListMouseListener(this);
- }
-
- protected MouseMotionListener createListMouseMotionListener() {
- return new ListMouseMotionListener(this);
- }
-
- protected ListSelectionListener createListSelectionListener() {
- return new ListSelListener(this);
- }
-
- protected MouseListener createMouseListener() {
- return new InvocationMouseListener(this);
- }
-
- protected MouseMotionListener createMouseMotionListener() {
- return new InvocationMouseMotionListener(this);
- }
-
- protected PropertyChangeListener createPropertyChangeListener() {
- return new ComboPropertyChangeListener(this);
- }
-
- protected JScrollPane createScroller() {
- return new JScrollPane(this.list, 20, 31);
- }
-
- protected void delegateFocus(MouseEvent e) {
- if (this.comboBox.isEditable()) {
- this.comboBox.getEditor().getEditorComponent().requestFocus();
- } else {
- this.comboBox.requestFocus();
- }
-
- }
-
- private Dialog getDialog() {
- Container parent;
- for(parent = this.comboBox.getParent(); parent != null && !(parent instanceof Dialog) && !(parent instanceof Window); parent = ((Component)parent).getParent()) {
- }
-
- return parent instanceof Dialog ? (Dialog)parent : null;
- }
-
- public KeyListener getKeyListener() {
- return this.keyListener;
- }
-
- public MouseListener getMouseListener() {
- return this.mouseListener;
- }
-
- public MouseMotionListener getMouseMotionListener() {
- return this.mouseMotionListener;
- }
-
- protected int getPopupHeightForRowCount(int maxRowCount) {
- int currentElementCount = this.comboBox.getModel().getSize();
- if (currentElementCount > 0) {
- Rectangle r = this.list.getCellBounds(0, 0);
- return maxRowCount < currentElementCount ? r.height * maxRowCount + 2 : r.height * currentElementCount + 2;
- } else {
- return 100;
- }
- }
-
- public void hide() {
- ((JPopupMenu)this).setVisible(false);
- this.comboBox.repaint();
- }
-
- private boolean inModalDialog() {
- return this.getDialog() != null;
- }
-
- public boolean isFocusTraversable() {
- return false;
- }
-
- public void show() {
- Dimension popupSize = this.comboBox.getSize();
- popupSize.setSize(popupSize.width, this.getPopupHeightForRowCount(this.comboBox.getMaximumRowCount()));
- this.scroller.setMaximumSize(popupSize);
- this.scroller.setPreferredSize(popupSize);
- this.scroller.setMinimumSize(popupSize);
- Rectangle popupBounds = this.computePopupBounds(0, this.comboBox.getBounds().height, popupSize.width, popupSize.height);
- this.list.invalidate();
- this.list.setSelectedIndex(this.comboBox.getSelectedIndex());
- this.list.ensureIndexIsVisible(this.list.getSelectedIndex());
- ((JPopupMenu)this).setLightWeightPopupEnabled(this.comboBox.isLightWeightPopupEnabled());
- ((JPopupMenu)this).show(this.comboBox, popupBounds.x, popupBounds.y);
- }
-
- protected void startAutoScrolling(int direction) {
- if (this.isAutoScrolling) {
- this.autoscrollTimer.stop();
- }
-
- this.isAutoScrolling = true;
- if (direction == 0) {
- this.scrollDirection = 0;
- Point convertedPoint = SwingUtilities.convertPoint(this.scroller, new Point(1, 1), this.list);
- int top = this.list.locationToIndex(convertedPoint);
- this.valueIsAdjusting = true;
- this.list.setSelectedIndex(top);
- this.valueIsAdjusting = false;
- AbstractAction timerAction = new 1(this);
- this.autoscrollTimer = new Timer(100, timerAction);
- } else if (direction == 1) {
- this.scrollDirection = 1;
- Dimension size = this.scroller.getSize();
- Point convertedPoint = SwingUtilities.convertPoint(this.scroller, new Point(1, size.height - 1 - 2), this.list);
- int bottom = this.list.locationToIndex(convertedPoint);
- this.valueIsAdjusting = true;
- this.list.setSelectedIndex(bottom);
- this.valueIsAdjusting = false;
- AbstractAction timerAction = new 2(this);
- this.autoscrollTimer = new Timer(100, timerAction);
- }
-
- this.autoscrollTimer.start();
- }
-
- protected void stopAutoScrolling() {
- this.isAutoScrolling = false;
- if (this.autoscrollTimer != null) {
- this.autoscrollTimer.stop();
- this.autoscrollTimer = null;
- }
-
- }
-
- protected void togglePopup() {
- if (((JPopupMenu)this).isVisible()) {
- this.hide();
- } else {
- this.show();
- }
-
- }
-
- public void uninstallingUI() {
- this.comboBox.removePropertyChangeListener(this.propertyChangeListener);
- this.comboBox.removeItemListener(this.itemListener);
- }
-
- protected void updateListBoxSelectionForEvent(MouseEvent anEvent, boolean shouldScroll) {
- Point location = anEvent.getPoint();
- if (this.list != null) {
- int index = this.list.locationToIndex(location);
- if (index == -1) {
- if (location.y < 0) {
- index = 0;
- } else {
- index = this.comboBox.getModel().getSize() - 1;
- }
- }
-
- if (this.list.getSelectedIndex() != index) {
- this.list.setSelectedIndex(index);
- if (shouldScroll) {
- this.list.ensureIndexIsVisible(index);
- }
- }
-
- }
- }
- }
-